#include <cctype>
#include <cassert>
#include <cstring>
#include <strings.h>

/* Copy a string from src to dest, returning a pointer to
   the end of the resulting string at dest. */
char *mystpcpy(char *dest, const char *src)
{
    while(*src)
    {
        *dest=*src;
        ++dest;
        ++src;
    }
    return dest;
}

/* Compare the strings s1 and s2 ignoring case. */
int mystrcasecmp(const char *s1, const char *s2)
{
    while(*s1 and *s2)
    {
        if (toupper(*s1)!=toupper(*s2)){
            break;
        }
        ++s1;
        ++s2;
    }
    return toupper(*s1)-toupper(*s2);
}

/* Compare the first n bytes of the strings s1 and s2 ignoring case. */
int mystrncasecmp(const char *s1, const char *s2, size_t n)
{
    while(*s1 and *s2 and n)
    {
        if (toupper(*s1)!=toupper(*s2)){
            break;
        }
        ++s1;
        ++s2;
        n--;
    }
    return toupper(*s1)-toupper(*s2);
}

int main()
{
    char buf1[100], buf2[100];
    const char *hello1 = "Hello, world!";

    char *result = mystpcpy(buf1, hello1);
    char *expected = ::stpcpy(buf2, hello1);

    assert(result == (buf1 + strlen(hello1)));
    assert(expected == (buf2 + strlen(hello1)));
    assert(::strcmp(buf1, buf2) == 0);

    const char *hello2 = "hello, world.";
    assert(mystrcasecmp(hello1, hello2) == ::strcasecmp(hello1, hello2));
    assert(mystrncasecmp(hello1, hello2, 5) == ::strncasecmp(hello1, hello2, 5));
}
